#coding:utf8
"""
Created on May 4, 2013

@author: join
"""

from cvirt.manager.monitor.plugins.cpu import CPU
import os
from cvirt.manager.monitor.plugins.df import Df
from cvirt.manager.monitor.plugins.disk import Disk
from cvirt.manager.monitor.plugins.interface import Interface
from cvirt.manager.monitor.plugins.load import Load
from cvirt.manager.monitor.plugins.memory import Memory
from cvirt.manager.monitor.plugins.swap import Swap
import datetime
from cvirt.manager.monitor.plugins.libvirt import VCPU, VDisk, VInterface
from cvirt.manager.monitor.graph import Grapher

class PluginTestSuite(object):
    def __init__(self):
        self.data_dir = '/var/lib/collectd/rrd/' #rrd文件路径
        self.graph_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),'static','img/monitor')
    
    def gen_time_range(self,starttime,endtime):
        init_time = datetime.datetime(1970,1,1,0,0,0)
        epoch_start = datetime.datetime.strptime(starttime,"%Y-%m-%d %H:%M:%S")
        epoch_end = datetime.datetime.strptime(endtime,"%Y-%m-%d %H:%M:%S")
        
        starttime = int((epoch_start-init_time).total_seconds())-28800
        endtime = int((epoch_end-init_time).total_seconds())-28800
        
        return "%s:%s"%(starttime,endtime)
    
    def test_graph_cpu(self,machine,time='hour'):
#        starttime = '2013-05-06 10:40:00'
#        endtime = '2013-05-06 14:00:00'
#        time = self.gen_time_range(starttime,endtime)
        
        self.cpu_data_dir = os.path.join(self.data_dir,machine)
        self.cpu_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.graph_dir):
            os.makedirs(self.graph_dir)
        cpu = CPU(self.cpu_data_dir,self.cpu_graph_dir)
        cpu.set_time(time)
#        cpu.set_comment("从%s 到 %s的监控图像"%(starttime.replace(":", "\:"),endtime.replace(":", "\:")))
        result = cpu.gen()
        print result
        
    def test_graph_df(self,machine,time='day'):
        self.df_data_dir = os.path.join(self.data_dir,machine)
        self.df_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.df_graph_dir):
            os.makedirs(self.df_graph_dir)
        df = Df(self.df_data_dir,self.df_graph_dir)
        df.set_time(time)
        df.gen()
        
    def test_graph_disk(self,machine,time='hour'):
        self.disk_data_dir = os.path.join(self.data_dir,machine)
        self.disk_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.disk_graph_dir):
            os.makedirs(self.disk_graph_dir)
        disk = Disk(self.disk_data_dir,self.disk_graph_dir)
        disk.set_time(time)
        disk.gen()
        
    def test_graph_interface(self,machine,time='hour'):
        self.interface_data_dir = os.path.join(self.data_dir,machine)
        self.interface_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.interface_graph_dir):
            os.makedirs(self.interface_graph_dir)
        interface = Interface(self.interface_data_dir,self.interface_graph_dir)
        interface.set_time(time)
        interface.gen()
        
    def test_graph_load(self,machine,time='hour'):
        self.load_data_dir = os.path.join(self.data_dir,machine)
        self.load_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.load_graph_dir):
            os.makedirs(self.load_graph_dir)
        load = Load(self.load_data_dir,self.load_graph_dir)
        load.set_time(time)
        load.gen()
        
    def test_graph_memory(self,machine,time='hour'):
        self.memory_data_dir = os.path.join(self.data_dir,machine)
        self.memory_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.memory_graph_dir):
            os.makedirs(self.memory_graph_dir)
        memory = Memory(self.memory_data_dir,self.memory_graph_dir)
        memory.set_time(time)
        memory.gen()
        
    def test_graph_swap(self,machine,time='hour'):
        self.swap_data_dir = os.path.join(self.data_dir,machine)
        self.swap_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.swap_graph_dir):
            os.makedirs(self.swap_graph_dir)
        swap = Swap(self.swap_data_dir,self.swap_graph_dir)
        swap.set_time(time)
        swap.gen()
        
    def test_graph_vcpu(self,machine,time='hour'):
        self.swap_data_dir = os.path.join(self.data_dir,machine)
        self.swap_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.swap_graph_dir):
            os.makedirs(self.swap_graph_dir)
        swap = VCPU(self.swap_data_dir,self.swap_graph_dir)
        starttime = '2013-05-06 16:40:00'
        endtime = '2013-05-06 17:00:00'
        time = self.gen_time_range(starttime,endtime)
        swap.set_time(time)
        result = swap.gen()
        print result
        
    def test_graph_vdisk(self,machine,time='hour'):
        self.swap_data_dir = os.path.join(self.data_dir,machine)
        self.swap_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.swap_graph_dir):
            os.makedirs(self.swap_graph_dir)
        swap = VDisk(self.swap_data_dir,self.swap_graph_dir)
        starttime = '2013-05-06 16:40:00'
        endtime = '2013-05-06 17:00:00'
        time = self.gen_time_range(starttime,endtime)
        swap.set_time(time)
        result = swap.gen()
        
    def test_graph_vinterface(self,machine,time='hour'):
        self.swap_data_dir = os.path.join(self.data_dir,machine)
        self.swap_graph_dir = os.path.join(self.graph_dir,machine)
        if not os.path.exists(self.swap_graph_dir):
            os.makedirs(self.swap_graph_dir)
        swap = VInterface(self.swap_data_dir,self.swap_graph_dir)
#        starttime = '2013-05-06 16:40:00'
#        endtime = '2013-05-06 17:00:00'
#        time = self.gen_time_range(starttime,endtime)
        swap.set_time(time)
        result = swap.gen()

def run():
    test = PluginTestSuite()
#    test.test_graph_vinterface('ubuntu')
    test.test_graph_cpu('kangqiang')
#    test.test_graph_df('tcloud247')
#    test.test_graph_disk('kangqiang')
#    test.test_graph_interface('kangqiang')
#    test.test_graph_load('kangqiang')
#    test.test_graph_memory('tcloud')
#    test.test_graph_swap('kangqiang')
    
#run()

if __name__ == "__main__":
    hostname = 'kangqiang'
    metric = ''
    starttime = ''
    endtime = ''
    timerange = 'day'
    grapher = Grapher(hostname,metric,starttime,endtime,timerange)
    result = grapher.graph()
    
    